#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/7/18 22:48
# @Author : cjw
from jsonpath import jsonpath

from server_task.api.wework_api import WeWorkApi


class DepartmentApi(WeWorkApi):

    def get(self, dp_id=None, **kwargs):
        """
        获取部门列表
        :param dp_id: 部门id
        :return:
        """
        if 'json' in kwargs:
            json_data = kwargs.get('json')
            json_data.update(access_token=self.token)
        else:
            json_data = {'id': dp_id, 'access_token': self.token}

        data = {
            'method': 'get',
            'url': f'{self.base_url}/department/list',
            'params': json_data
        }
        return self.request(data)

    def create(self, name, parentid, **kwargs):
        """
        创建部门
        :param name: 部门名称
        :param parentid:  父部门id
        :param kwargs:
        :return:
        """
        if 'json' in kwargs:
            json_data = kwargs.get('json')
        else:
            json_data = {'name': name, 'parentid': parentid}

        data = {
            'method': 'post',
            'url': f'{self.base_url}/department/create',
            'params': {'access_token': self.token},
            'json': json_data
        }
        return self.request(data)

    def update(self, dp_id, name=None, **kwargs):
        """
        更新部门
        :param dp_id: 部门id
        :param name: 部门名称
        :param kwargs:
        :return:
        """
        if 'json' in kwargs:
            json_data = kwargs.get('json')
        else:
            json_data = {'id': dp_id, 'name': name}

        data = {
            'method': 'post',
            'url': f'{self.base_url}/department/update',
            'params': {'access_token': self.token},
            'json': json_data
        }
        return self.request(data)

    def delete(self, dp_id, **kwargs):
        """
        删除部门
        :param dp_id: 部门id
        :param kwargs:
        :return:
        """
        if 'json' in kwargs:
            json_data = kwargs.get('json')
            json_data.update(access_token=self.token)
        else:
            json_data = {'id': dp_id, 'access_token': self.token}

        data = {
            'method': 'get',
            'url': f'{self.base_url}/department/delete',
            'params': json_data
        }
        return self.request(data)

    def department_names(self, dp_id=None) -> list:
        """
        获取部门名称列表
        :param dp_id: 部门id
        :return:
        """
        return jsonpath(self.get(dp_id=dp_id).json(), '$..name')

    def department_name_id(self, is_parent_id: bool = False) -> dict:
        """
        获取部门名称和id字典，默认是获取部门id
        {'name': id}
        :param is_parent_id: 是否获取父部门id
        :return:
        """
        result = self.get().json()
        if is_parent_id:
            return dict(zip(jsonpath(result, '$..department[*].name'), jsonpath(result, '$..department[*].parentid')))
        return dict(zip(jsonpath(result, '$..department[*].name'), jsonpath(result, '$..department[*].id')))
